// SynapseHeadTest.cpp : Defines the entry point for the console application.
//

#ifndef _WIN32_WINNT            
#define _WIN32_WINNT 0x0500     // Change this to the appropriate value to target other versions of Windows.
#endif

#include <stdio.h>
#include <tchar.h>
#include "synapse.h"

int _tmain(int argc, _TCHAR* argv[])
{
	if (Synapse<long>::Exists("Head-X"))
	{
		Synapse<long> myHeadVersion;
		myHeadVersion.Open("Head-X(1)");
		printf("Head X detected: version %d\n", myHeadVersion.ReadCopy());

		Synapse<long> myHeadStatus;
		myHeadStatus.Open("Head-X(1)-Status");
		myHeadStatus.SetAsRead();

		Synapse<char> myHeadCommand;
		myHeadCommand.Open("Head-X(1)-Command");

		// -----
		//while (true) {
		/*myHeadCommand.ProtectWriteArray("Hello user. Please {emote wink} observe me carefully. {br} {br 0.2} I am being tested.");
				
		printf("Speaking");
		myHeadStatus.WaitForNew();
		while (myHeadStatus.ReadCopy(1) > 0)
		{
			printf(".");
			Sleep(250);
		}
		printf("\nDone speaking?\n");
		//}*/

		// -----

		Synapse<float> myHeadCamera;
		myHeadCamera.Open("Head-X(1)-Camera");
		float cameraBuffer[30];
		printf("Rotating");
		/*for (int i = 0; i < 600; i++)
		{
			myHeadCamera.ReadArrayWhenNew(cameraBuffer);
			cameraBuffer[2] += 0.01f;
			cameraBuffer[11] -= 0.02f;
			myHeadCamera.ProtectWriteArray(cameraBuffer);
			//myHeadCamera.SetAsRead();
			printf(".");
		}*/
		printf("\nDone rotating!\n");

		Synapse<float> myHeadMorphs;
		myHeadMorphs.Open("Head-X(1)-HeadMorphs");
		float morphBuffer[100];
		printf("Morphing");
		for (int i = 0; i < 100; i++)
		{
			myHeadMorphs.ReadArrayWhenNew(morphBuffer);
			morphBuffer[1] += 0.02f;
			morphBuffer[11] -= 0.02f;
			myHeadMorphs.ProtectWriteArray(morphBuffer);
			//myHeadMorphs.SetAsRead();
			printf(".");
		}
		for (int i = 0; i < 100; i++)
		{
			myHeadMorphs.ReadArrayWhenNew(morphBuffer);
			morphBuffer[1] -= 0.02f;
			morphBuffer[11] += 0.02f;
			myHeadMorphs.ProtectWriteArray(morphBuffer);
			//myHeadMorphs.SetAsRead();
			printf(".");
		}
		printf("\nDone morphing!\n");

		// -----

		Synapse<char> myHeadSpoken;
		myHeadSpoken.Open("Head-X(1)-SpokenText");
		myHeadSpoken.SetAsRead();

		Synapse<char> myHeadDirect;
		myHeadDirect.Open("Head-X(1)-DirectCommand");
		Sleep(1000);
		myHeadDirect.ProtectWriteArray("emote anger 3.0");
		myHeadDirect.ProtectWriteArray("emote ears 1.0");
		myHeadCommand.ProtectWriteArray("I stop speaking, probably early, with an expressive blink.");
		Sleep(1000);
		myHeadDirect.ProtectWriteArray("emote blink 2.0");
		myHeadStatus.WaitForMatch(0, 1);
		
		// -----
/*
		myHeadCommand.ProtectWriteArray("I am saying something of little interest.");
		Sleep(500);
		myHeadCommand.ProtectWriteArray("An interruption has occurred.");
		myHeadStatus.WaitForMatch(0, 1);

		// -----
		
		myHeadCommand.ProtectWriteArray("I am talking while I am being deleted!");
		myHeadDirect.ProtectWriteArray("clearconfig");
		myHeadStatus.WaitForMatch(1, 0);
		myHeadStatus.WaitForMatch(0, 1);
		
		myHeadCommand.ProtectWriteArray("{loadconfig hxdebug.hxml} I am talking while I am being loaded!");
		myHeadStatus.WaitForMatch(1, 0);
		myHeadStatus.WaitForMatch(0, 1);

		myHeadCommand.ProtectWriteArray("OK, that was easy.");
		myHeadStatus.WaitForMatch(0, 1);

		myHeadCommand.ProtectWriteArray("I am loading a background now. {loadbg ../Data/hxlogo.png} That should be easy, too.");
		myHeadStatus.WaitForMatch(0, 1);
		myHeadCommand.ProtectWriteArray("And it's gone again.");
		Sleep(1000);
		myHeadDirect.ProtectWriteArray("clearbg");
		myHeadStatus.WaitForMatch(1, 0);

		myHeadCommand.ProtectWriteArray("{morphfg 3 ../Data/FG/tough.fg} I am a {em +grin} tough guy {br} {morphfg 1.5 ../Data/FG/Martin.fg} I am not so tough, am I now?");
		myHeadStatus.WaitForMatch(0, 1);
		myHeadCommand.ProtectWriteArray("I'm done!");
		Sleep(1000);
		myHeadDirect.ProtectWriteArray("morphfg 2 ../Data/FG/default.fg");
		myHeadDirect.ProtectWriteArray("em -grin");*/

		// -----

	}
	else
	{
		printf("No Head X instance detected.\n");
	}


	return 0;
}

